/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import database.OrderDAO;
import database.StaffDAO;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.*;
import model.Order;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.validator.annotations.*;
import database.IssuingDAO;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Order")
public class WOrder extends ActionSupport implements ModelDriven<Issue>{
    private List<Order> orders = new ArrayList<Order>();
    private List<Orderitem> orderitems = new ArrayList<Orderitem>();
    private Integer idStaff;
    private Integer oid;
    private Integer hidenID;
    private Issue issue;
    private String createDate;
    private List<Staff> staffs;
    private Staff staff;

    
    public StaffDAO staffDAO = new StaffDAO();
    private Map<Integer, String> staffsName;
    public OrderDAO orderDAO = new OrderDAO();
    public IssuingDAO issueDAO = new IssuingDAO();
    private Map<Integer,String> fills;
    private Integer fill;
    
    public WOrder()
    {
        staffsName = new HashMap<Integer, String>();
        staffs = staffDAO.getAllStaff();
        
        for(Staff item: staffs)
        {
            staffsName.put(item.getIdStaff(), item.getFirstname()+" "+item.getLastname());
        }
        fills = new HashMap<Integer, String>();
        fills.put(1, "Order Status - Finished");
        fills.put(2, "Order Status - Processing");
        fills.put(3, "Order Status - Paypal - Completed");     
        fills.put(4, "Order Status - Shipped");
    }

    public Map<Integer, String> getFills() {
        return fills;
    }

    public void setFills(Map<Integer, String> fills) {
        this.fills = fills;
    }

    public Integer getFill() {
        return fill;
    }

    public void setFill(Integer fill) {
        this.fill = fill;
    }
    
    public Staff getStaff() {
        return staff;
    }

    public void setStaff(Staff staff) {
        this.staff = staff;
    }
    
    public Integer getIdStaff() {
        return idStaff;
    }

    public void setIdStaff(Integer idStaff) {
        this.idStaff = idStaff;
    }  

    
    public Map<Integer, String> getStaffsName() {
        return staffsName;
    }

    public void setStaffsName(Map<Integer, String> staffsName) {
        this.staffsName = staffsName;
    }

    public List<Staff> getStaffs() {
        return staffs;
    }

    public void setStaffs(List<Staff> staffs) {
        this.staffs = staffs;
    }

    

    public String getCreateDate() {
        return createDate;
    }
    
    
    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }
    
    public List<Orderitem> getOrderitems() {
        return orderitems;
    }

    public void setOrderitems(List<Orderitem> orderitems) {
        this.orderitems = orderitems;
    }
    private Order order;

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
    private Orderitem orderitem;

    public Orderitem getOrderitem() {
        return orderitem;
    }

    public void setOrderitem(Orderitem orderitem) {
        this.orderitem = orderitem;
    }
    

    public Issue getIssue() {
        return issue;
    }

    public void setIssue(Issue issue) {
        this.issue = issue;
    }
    

    @Action(value = "order-management", results = {
        @Result(name = "success", location = "/manageOrder/w_order.jsp")
    })
    public String OrderManagement(){
        Map session = ActionContext.getContext().getSession();
                session.put("order", 1);
        orders = orderDAO.getAllOrder();
        int[] idOrder = new int[orders.size()];
        String[] status = new String[orders.size()];
        int index = 0;
        for(Order item: orders)
        {
            idOrder[index] = item.getIdOrder();
            status[index] = item.getOrderStatus();
            index ++;
        }
          session.put("idOrder", idOrder);
          session.put("status", status);
          String[] idOrder2 = (String[])session.get("status");
        return SUCCESS;
    }
    @Action(value = "fill-order", results = {
        @Result(name = "success", location = "/manageOrder/w_order.jsp")
    })
    public String Sort(){
         Map session = ActionContext.getContext().getSession();
                session.put("order", 2);
                
        orders = orderDAO.getOrderByCreateStatus(fill);
        int[] idOrder = new int[orders.size()];
        String[] status = new String[orders.size()];
        int index = 0;
        for(Order item: orders)
        {
            //System.out.println("IDORder" + item.getIdOrder());
            idOrder[index] = item.getIdOrder();
            status[index] = item.getOrderStatus();
            index ++;
        }
          session.put("idOrder", idOrder);
          session.put("status", status);
        
        return SUCCESS;
    }
     @Action(
            value = "create-issuing/*",
            results = {
        @Result(name = "success", location = "/manageOrder/w_issueFromOrder.jsp")},
            params = {"oid", "{1}"})
    public String CreateIssuePage() {
          Map session = ActionContext.getContext().getSession();
                
         //hiddentID = oid;
         setHidenID(getOid());
         order= orderDAO.getOrderbyID(oid);
         
         orderitems = orderDAO.getOrderitemByOrderId(oid);
         session.put("orderModel", order);
         session.put("itemModel", orderitems);
        
        return SUCCESS;
    }
    @Action(
            value = "createissue",
            results = {
        @Result(name = "success", location = "/manageIssue/issuedetail.jsp")
            })
    public String CreateIssue() throws ParseException {  
         SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
         Map session = ActionContext.getContext().getSession();
                session.put("order", 1);
        //System.out.println(hidenID);
        Date start = dateFormat.parse(createDate);
        if(issueDAO.InsertIssue(hidenID, idStaff, start));
        if(orderDAO.updateOrderStatus(hidenID));
        List<Orderitem> tempOrderitems = orderDAO.getOrderitemByOrderId(hidenID);
        Product product = new Product();
        int orderQuantity;
        for(Orderitem item: tempOrderitems)
        {
            product = item.getProduct();
            orderQuantity = item.getOrderQuantity();
            orderDAO.UpdateProductStockQuantity(product, (product.getStockQuantity() - orderQuantity));
        }
        
        orders = orderDAO.getAllOrder();
        int[] idOrder = new int[orders.size()];
        String[] status = new String[orders.size()];
        int index = 0;
        for(Order item: orders)
        {
            //System.out.println("IDORder" + item.getIdOrder());
            idOrder[index] = item.getIdOrder();
            status[index] = item.getOrderStatus();
            index ++;
        }
          session.put("idOrder", idOrder);
          session.put("status", status);
          order = (Order) session.get("orderModel");
          orderitems = (List<Orderitem>) session.get("itemModel");
        return SUCCESS;
        
    }
    /**
     * @return the orders
     */
    public List<Order> getOrders() {
        return orders;
    }

    /**
     * @param orders the orders to set
     */
    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }

    /**
     * @return the oid
     */
    public Integer getOid() {
        return oid;
    }

    /**
     * @param oid the oid to set
     */
    public void setOid(Integer oid) {
        this.oid = oid;
    }

    /**
     * @return the hidenID
     */
    public Integer getHidenID() {
        return hidenID;
    }

    /**
     * @param hidenID the hidenID to set
     */
    public void setHidenID(Integer hidenID) {
        this.hidenID = hidenID;
    }

    @Override
    public Issue getModel() {
        return issue; //To change body of generated methods, choose Tools | Templates.
    }
}
